package com.shinhye.smartsms;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends TabActivity implements OnClickListener{
	private final String TAG_CONVERSATION = "conversation";
	private final String TAG_FOLDER = "folder";
	private final String TAG_GROUP = "group";
    private TabHost tabhost;
	private LinearLayout ll_conversation;
	private LinearLayout ll_group;
	private LinearLayout ll_folder;
	private View v_slide;
	private int currentTab;
	private RelativeLayout rl;
	private int tabWidth;

	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
//        tabhost = getTabHost();
        tabhost = (TabHost) findViewById(android.R.id.tabhost);
        ll_conversation = (LinearLayout) findViewById(R.id.ll_conversation);
        ll_folder = (LinearLayout) findViewById(R.id.ll_folder);
        ll_group = (LinearLayout) findViewById(R.id.ll_group);
        rl = (RelativeLayout) findViewById(R.id.rl);
        v_slide = findViewById(R.id.v_slide);
        
        currentTab = 0;
        init();
    }

	private void init() {
		addTab(TAG_CONVERSATION, "会话", R.drawable.tab_conversation, new Intent(this, ConversationActivity.class));
		addTab(TAG_FOLDER, "文件夹", R.drawable.tab_folder, new Intent(this, FolderActivity.class));
		addTab(TAG_GROUP, "群组", R.drawable.tab_group, new Intent(this, GroupActivity.class));
		
		ll_conversation.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
			@Override
			public void onGlobalLayout() {
				ll_conversation.getViewTreeObserver().removeGlobalOnLayoutListener(this);
				int width = ll_conversation.getWidth();
				int height = ll_conversation.getHeight();
				int left = ll_conversation.getLeft();
				LayoutParams params = (LayoutParams) v_slide.getLayoutParams();
				params.width = width;
				params.height = height;
				params.leftMargin = left;
				v_slide.setLayoutParams(params);
				tabWidth = rl.getWidth();
			}
		});
		
		ll_conversation.setOnClickListener(this);
		ll_folder.setOnClickListener(this);
		ll_group.setOnClickListener(this);
	}

	private void addTab(String tag, String label, int iconId, Intent intent) {
		TabSpec tabSpec = tabhost.newTabSpec(tag);
		tabSpec.setIndicator(label, getResources().getDrawable(iconId));
		tabSpec.setContent(intent);
		
		tabhost.addTab(tabSpec);
	}
	
	private void startAnim(float fromXDelta, float toXDelta) {
		TranslateAnimation ta = new TranslateAnimation(fromXDelta, toXDelta, 0, 0);
		ta.setDuration(400);
		ta.setFillAfter(true);
		v_slide.startAnimation(ta);
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()) {
		case R.id.ll_conversation:
			tabhost.setCurrentTabByTag(TAG_CONVERSATION);
			startAnim(currentTab * tabWidth, 0);
			currentTab = 0;
			break;
		case R.id.ll_folder:
			tabhost.setCurrentTabByTag(TAG_FOLDER);
			startAnim(currentTab * tabWidth, tabWidth);
			currentTab = 1;
			break;
		case R.id.ll_group:
			tabhost.setCurrentTabByTag(TAG_GROUP);
			startAnim(currentTab * tabWidth, 2 * tabWidth);
			currentTab = 2;
			break;
		}
	}
}
